package com.epam.facebook_client.content;

import java.util.List;

import com.epam.facebook_client.content.contract.AppContract;
import com.epam.facebook_client.content.db.DbOpenHelper;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.util.Log;

public class FacebookProvider extends ContentProvider {

	DbOpenHelper dbOpenHelper;

	private static final int FRIENDS = 1;
	private static final int FRIENDS_ID = 2;
	private static final int FEEDS = 3;
	private static final int FEEDS_ID = 4;
	private static final int ALBUMS = 5;
	private static final int ALBUMS_ID = 6;
	private static final int PHOTOS = 7;
	private static final int PHOTOS_ID = 8;

	private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);

	private static final String TAG = FacebookProvider.class.getSimpleName();

	static {
		sURIMatcher.addURI(AppContract.AUTHORITY, AppContract.FriendsColumns.CONTENT_PATH, FRIENDS);
		sURIMatcher.addURI(AppContract.AUTHORITY, AppContract.FriendsColumns.CONTENT_PATH + "/#", FRIENDS_ID);
		sURIMatcher.addURI(AppContract.AUTHORITY, AppContract.FeedColumns.CONTENT_PATH, FEEDS);
		sURIMatcher.addURI(AppContract.AUTHORITY, AppContract.FeedColumns.CONTENT_PATH + "/#", FEEDS_ID);
		sURIMatcher.addURI(AppContract.AUTHORITY, AppContract.AlbumColumns.CONTENT_PATH, ALBUMS);
		sURIMatcher.addURI(AppContract.AUTHORITY, AppContract.AlbumColumns.CONTENT_PATH + "/#", ALBUMS_ID);
		sURIMatcher.addURI(AppContract.AUTHORITY, AppContract.PhotosColumns.CONTENT_PATH, PHOTOS);
		sURIMatcher.addURI(AppContract.AUTHORITY, AppContract.PhotosColumns.CONTENT_PATH + "/#", PHOTOS_ID);
	}

	@Override
	public boolean onCreate() {
		dbOpenHelper = new DbOpenHelper(getContext(), null, null, 0);
		if (dbOpenHelper != null) {
			return true;
		}
		return false;
	}

	@Override
	public String getType(Uri uri) {
		switch (sURIMatcher.match(uri)) {
		case FRIENDS:
			return AppContract.FriendsColumns.CONTENT_TYPE;
		case FRIENDS_ID:
			return AppContract.FriendsColumns.CONTENT_ITEM_TYPE;
		case FEEDS:
			return AppContract.FeedColumns.CONTENT_TYPE;
		case FEEDS_ID:
			return AppContract.FeedColumns.CONTENT_ITEM_TYPE;
		case ALBUMS:
			return AppContract.AlbumColumns.CONTENT_TYPE;
		case ALBUMS_ID:
			return AppContract.AlbumColumns.CONTENT_ITEM_TYPE;
		case PHOTOS:
			return AppContract.AlbumColumns.CONTENT_TYPE;
		case PHOTOS_ID:
			return AppContract.AlbumColumns.CONTENT_ITEM_TYPE;
		default:
			throw new IllegalArgumentException("Unsupported URI: " + uri);
		}
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int delete = dbOpenHelper.deleteItem(getTableByUri(uri), selection, selectionArgs);
		Log.i(TAG, "Delete: " + delete);
		return delete;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		Uri _uri = null;
		long id = dbOpenHelper.insertItem(getTableByUri(uri), null, values);
		if (id > 0) {
			_uri = ContentUris.withAppendedId(uri, id);
			getContext().getContentResolver().notifyChange(_uri, null);
		}

		return _uri;
	}

	@Override
	public int bulkInsert(Uri uri, ContentValues[] values) {
		Uri _uri = null;
		List<Long> listId = dbOpenHelper.insertItems(getTableByUri(uri), null, values);
		for (Long id : listId) {
			if (id > 0) {
				_uri = ContentUris.withAppendedId(uri, id);
				getContext().getContentResolver().notifyChange(_uri, null);
			}
		}
		Log.i(TAG, "Insert: " + listId.size());
		return listId.size();
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		Cursor cursor = dbOpenHelper.getItems(getTableByUri(uri), projection, selection, selectionArgs, sortOrder);
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		int update = dbOpenHelper.updateItem(getTableByUri(uri), values, selection, selectionArgs);
		Log.i(TAG, "Update: " + update);
		return update;
	}

	private String getTableByUri(Uri uri) {
		switch (sURIMatcher.match(uri)) {
		case FRIENDS:
			return DbOpenHelper.FRIENDS_TABLE;
		case FEEDS:
			return DbOpenHelper.FEEDS_TABLE;
		case ALBUMS:
			return DbOpenHelper.ALBUMS_TABLE;
		case PHOTOS:
			return DbOpenHelper.PHOTOS_TABLE;
		default:
			throw new SQLException("Failed row into " + uri);
		}
	}
}
